﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace QunYing.DAL
{
    /// <summary>
    /// 数据访问类:Category
    /// </summary>
    public partial class Category
    {
        /// <summary>
        /// 根据父ID取子类别（根元素父ID为0）
        /// </summary>
        public DataTable GetChildCategory(string parentID, string DealerID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select 类别ID,类别名称,类别图片 from Category where 父类别ID=@父类别ID AND 经销商编号='" + DealerID + "' order by 显示顺序 ASC");
            SqlParameter[] parameters = {
					new SqlParameter("@父类别ID",parentID)
             };
            return DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];
        }

        /// <summary>
        /// 根据父ID取子类别（根元素父ID为0）
        /// </summary>
        public DataTable GetChildCategoryByCache(string parentID, string DealerID)
        {
            string CacheKey = "GetChildCategory-" + parentID + DealerID;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = GetChildCategory(parentID, DealerID);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (DataTable)objModel;
        }

        /// <summary>
        /// 根据子ID取父类别
        /// </summary>
        public DataTable GetParentCategory(string childID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@id",childID)
             };
            return DbHelperSQL.RunProcedure("P_QueryNode", parameters, "result").Tables[0];
        }

        /// <summary>
        /// 根据子ID取子类别
        /// </summary>
        public DataTable GetParentCategoryByCache(string childID)
        {
            string CacheKey = "GetParentCategory-" + childID;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = GetParentCategory(childID);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (DataTable)objModel;
        }

        /// <summary>
        /// 根据父ID取子孙类别
        /// </summary>
        public DataTable GetChildsCategory(string parentID)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@id",parentID)
             };
            return DbHelperSQL.RunProcedure("P_QueryChildNode", parameters, "result").Tables[0];
        }

        /// <summary>
        /// 根据父ID取子孙类别
        /// </summary>
        public DataTable GetChildsCategoryByCache(string parentID)
        {
            string CacheKey = "GetChildsCategory-" + parentID;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = GetChildsCategory(parentID);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (DataTable)objModel;
        }
        /// <summary>
        /// 删除类别
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelCategory(string id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Category ");
            strSql.Append(" where 类别ID=@类别ID OR 父类别ID=@类别ID");
            SqlParameter[] parameters = {
					new SqlParameter("@类别ID",id)
};
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            return rows;
        }
    }
}

